package com.coffee.sku.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.sku.api.sku.query.SkuSalesInformationQuery;
import com.coffee.sku.api.sku.result.SkuSalesInformationResult;
import com.coffee.sku.api.sku.service.SkuSalesInformationService;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SkuSalesInformationServiceImpl implements SkuSalesInformationService {
	@Resource
	private SkuSalesInformationDao	skuSalesInformationDao;

	@Override
	public Page<SkuSalesInformationResult> getPager(SkuSalesInformationQuery param, PagerInfo pr) {
		Page<SkuSalesInformationResult> pager=new Page<SkuSalesInformationResult>(pr.getStart(), pr.getPageSize());
		Integer count=skuSalesInformationDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SkuSalesInformation> list=skuSalesInformationDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SkuSalesInformationResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SkuSalesInformationQuery param){
		Integer count=skuSalesInformationDao.count(param);
		return count;
	}
	
	@Override
	public SkuSalesInformationResult getDetail(Integer id){
		SkuSalesInformation item=skuSalesInformationDao.findById(id);
		if(item==null){
			return null;
		}
		SkuSalesInformationResult result=new SkuSalesInformationResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SkuSalesInformationResult> getList(SkuSalesInformationQuery param, PagerInfo pr){
		List<SkuSalesInformation> list=skuSalesInformationDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SkuSalesInformationResult.class);
	}
	
	@Override
	public List<SkuSalesInformationResult> getList(SkuSalesInformationQuery param){
		List<SkuSalesInformation> list=skuSalesInformationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuSalesInformationResult.class);
	}
	
	@Override
	public List<SkuSalesInformationResult> getList(){
		SkuSalesInformationQuery param = new SkuSalesInformationQuery();
		List<SkuSalesInformation> list=skuSalesInformationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuSalesInformationResult.class);
	}


	@Override
	public void delete(Integer id) {
		skuSalesInformationDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SkuSalesInformationResult result)throws BusinessException {
		SkuSalesInformation item=new SkuSalesInformation();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			skuSalesInformationDao.insert(item);
		}else {
			SkuSalesInformation tmp=skuSalesInformationDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			skuSalesInformationDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SkuSalesInformationResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SkuSalesInformationResult> map=new HashMap<Integer, SkuSalesInformationResult>();
		List<SkuSalesInformation> list=skuSalesInformationDao.findByIds(StringUtil.setToList(idSet));
		List<SkuSalesInformationResult> resultList=DTOUtils.convertList(list, SkuSalesInformationResult.class);
		for(SkuSalesInformationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SkuSalesInformationResult> getItemMap(){
		Map<Integer, SkuSalesInformationResult> map=new HashMap<Integer, SkuSalesInformationResult>();
		SkuSalesInformationQuery param=new SkuSalesInformationQuery();
		List<SkuSalesInformation> list=skuSalesInformationDao.page(param, 0, Integer.MAX_VALUE);
		List<SkuSalesInformationResult> resultList=DTOUtils.convertList(list, SkuSalesInformationResult.class);
		for(SkuSalesInformationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
